/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.information.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.information.domain.Slide;
import me.zhengjie.modules.information.domain.SlideItem;
import me.zhengjie.modules.information.domain.resp.SlideItemListResp;
import me.zhengjie.modules.information.mapper.SlideMapper;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.information.service.SlideItemService;
import me.zhengjie.modules.information.domain.vo.SlideItemQueryCriteria;
import me.zhengjie.modules.information.mapper.SlideItemMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

import static me.zhengjie.modules.biz.domain.enums.ErrorCode.BODY_NOTNULL;
import static me.zhengjie.modules.pay.enums.StatusEnum.SLIDE_SHOW;

/**
* @description 服务实现
* @author yiwei.he
* @date 2024-05-08
**/
@Service
@RequiredArgsConstructor
public class SlideItemServiceImpl extends ServiceImpl<SlideItemMapper, SlideItem> implements SlideItemService {

    private final SlideItemMapper slideItemMapper;
    private final SlideMapper slideMapper;

    @Override
    public PageResult<SlideItem> queryAll(SlideItemQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(slideItemMapper.findAll(criteria, page));
    }

    @Override
    public List<SlideItem> queryAll(SlideItemQueryCriteria criteria){
        return slideItemMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SlideItem resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SlideItem resources) {
        SlideItem slideItem = getById(resources.getId());
        slideItem.copy(resources);
        saveOrUpdate(slideItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<SlideItem> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SlideItem slideItem : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("幻灯片id", slideItem.getSlideId());
            map.put("状态,1:显示;0:隐藏", slideItem.getStatus());
            map.put("排序", slideItem.getSort());
            map.put("幻灯片名称", slideItem.getTitle());
            map.put("幻灯片图片", slideItem.getImage());
            map.put("幻灯片链接", slideItem.getUrl());
            map.put("友情链接打开方式", slideItem.getTarget());
            map.put("幻灯片描述", slideItem.getDescription());
            map.put("幻灯片内容", slideItem.getContent());
            map.put("扩展信息", slideItem.getMore());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public SlideItemListResp getSlideList(SlideItemQueryCriteria criteria) throws BizException {
        if(criteria.getSlideId() == null){
            throw new BizException(BODY_NOTNULL.getCode(),BODY_NOTNULL.getMessage());
        }
        Slide slide = slideMapper.selectOne(new QueryWrapper<Slide>().eq("id", criteria.getSlideId()));
        // 查询显示的轮播图
        List<SlideItem> slideItemList = slideItemMapper.selectList(new QueryWrapper<SlideItem>().eq("status",SLIDE_SHOW.getCode()).eq("slide_id", criteria.getSlideId()));
        SlideItemListResp resp = new SlideItemListResp();
        resp.setItemList(slideItemList);
        resp.setId(slide.getId());
        resp.setName(slide.getName());
        resp.setRemark(slide.getRemark());
        return resp;
    }
}